package com.hlhj.carview.aty

import android.content.Intent
import com.bumptech.glide.Glide
import com.hlhj.carview.BaseAty
import com.hlhj.carview.Iview.IdentityView
import com.hlhj.carview.Iview.UploadView
import com.hlhj.carview.R
import com.hlhj.carview.bean.MyBaseBean
import com.hlhj.carview.presenter.IdentityPresenter
import com.hlhj.carview.presenter.UploadPresenter
import com.hlhj.carview.utils.MyGlideEngin
import com.hlhj.carview.utils.ToastUtil
import com.huantansheng.easyphotos.EasyPhotos
import kotlinx.android.synthetic.main.aty_identit.*
import java.io.File

/**
 * Created by Never Fear   on 2018\12\20 0020.
Never More....
 */
class IdentityAty :BaseAty<IdentityView,IdentityPresenter>(), IdentityView, UploadView {

    override fun setCommitResult(myBaseBean: MyBaseBean) {
        ToastUtil.toast("提交成功",this)
        setResult(-1)
        finish()
    }

    override fun setUpResult(myBaseBean: MyBaseBean, tag: Int) {
        when(tag){
            TAKEPHOTO1->{
                idFront=myBaseBean.info.toString()

            }
            TAKEPHOTO2->{
                idBack=myBaseBean.info.toString()
            }
            TAKEPHOTO3->{
                qFront=myBaseBean.info.toString()
            }
            TAKEPHOTO4->{
                qBack=myBaseBean.info.toString()
            }
            TAKEPHOTO5->{
                driveFront=myBaseBean.info.toString()
            }
            TAKEPHOTO6->{
                driveBack=myBaseBean.info.toString()
            }
        }
    }

    private var idFront:String?=null
    private var idBack:String?=null
    private var qFront:String?=null
    private var qBack:String?=null

    private var driveFront:String?=null
    private var driveBack:String?=null

    private var uploadPresenter:UploadPresenter?=null

    override fun setUpResult(myBaseBean: MyBaseBean) {

    }

    override fun initView() {
    }

    override fun logicStart() {
    }

    private val TAKEPHOTO1: Int=1001
    private val TAKEPHOTO2: Int=1002
    private val TAKEPHOTO3: Int=1003
    private val TAKEPHOTO4: Int=1004
    private val TAKEPHOTO5: Int=1005
    private val TAKEPHOTO6: Int=1006

    override fun setListener() {
        btCommit.setOnClickListener {
            if (idFront==null||idBack==null||qFront==null||qBack==null||driveFront==null||driveBack==null){
                ToastUtil.toast("请完善信息",this)
            }else{
                presenter?.identify(idFront!!,idBack!!,qFront!!,qBack!!,driveFront!!,driveBack!!,this)
            }
        }
        btIdFront.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO1)
        }
        btIdBack.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO2)
        }
        btqFront.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO3)
        }
        btqBack.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO4)
        }
        btDriveFront.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO5)
        }
        btDriveBack.setOnClickListener {
            EasyPhotos.createAlbum(this, true, MyGlideEngin())//参数说明：上下文，是否显示相机按钮，[配置Glide为图片加载引擎](https://github.com/HuanTanSheng/EasyPhotos/wiki/12-%E9%85%8D%E7%BD%AEImageEngine%EF%BC%8C%E6%94%AF%E6%8C%81%E6%89%80%E6%9C%89%E5%9B%BE%E7%89%87%E5%8A%A0%E8%BD%BD%E5%BA%93)
                    .setFileProviderAuthority("hlhj.fhp.carview.photo")//参数说明：见下方`FileProvider的配置`
                    .start(TAKEPHOTO6)
        }

    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode!=-1)return
        val pathList = data?.getStringArrayListExtra(EasyPhotos.RESULT_PATHS)!!
        when(requestCode){
            TAKEPHOTO1->{
                uploadPresenter?.upload(File(pathList[0])!!,this@IdentityAty,TAKEPHOTO1)
                Glide.with(this).load(pathList[0]).into(btIdFront)
            }

            TAKEPHOTO2->{
                uploadPresenter?.upload(File(pathList[0]),this@IdentityAty,TAKEPHOTO2)
                Glide.with(this).load(pathList[0]).into(btIdBack)

            }

            TAKEPHOTO3->{
                uploadPresenter?.upload(File(pathList[0]),this@IdentityAty,TAKEPHOTO3)
                Glide.with(this).load(pathList[0]).into(btqFront)

            }

            TAKEPHOTO4->{
                uploadPresenter?.upload(File(pathList[0]),this@IdentityAty,TAKEPHOTO4)
                Glide.with(this).load(pathList[0]).into(btqBack)

            }

            TAKEPHOTO5->{
                uploadPresenter?.upload(File(pathList[0]),this@IdentityAty,TAKEPHOTO5)
                Glide.with(this).load(pathList[0]).into(btDriveFront)

            }
            TAKEPHOTO6->{
                uploadPresenter?.upload(File(pathList[0]),this@IdentityAty,TAKEPHOTO6)
                Glide.with(this).load(pathList[0]).into(btDriveBack)

            }

        }
    }
    override fun bindView(): IdentityView {
        return this
    }

    override fun createPresenter(): IdentityPresenter {
        uploadPresenter= UploadPresenter()
        uploadPresenter?.let {
            it.createModule()
            it.bindView(this@IdentityAty)
        }
        return IdentityPresenter()
    }

    override fun getContentID(): Int {
        return R.layout.aty_identit
    }
}